<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        [v-cloak] {
            display: none;
        }
    </style>
</head>
<body>
<h1>微博首页</h1>
<div id="app">
    <div v-cloak v-if="user!=''">欢迎{{user.nick}}回来!<img :src="user.url" width="50" height="50">
        <a href="#" @click="logout()">退出登录</a>
        <a href="send.html">发布微博</a>
        <a href="/myweibo.html">我的微博</a>
    </div>

    <div v-cloak v-if="user==''">
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>
    <hr>
    <!--遍历生成所有微博内容-->
    <div v-for="weibo in arr">
        <h3>
            <a :href="'/detail.html?id='+weibo.id">
                {{weibo.author}}说:{{weibo.content}}
            </a>
        </h3>
        <img :src="weibo.url" width="200" alt="">
    </div>
</div>
<!--引入Vue-->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<!--引入Axios-->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    let vm = new Vue({
        el: "#app",
        data: {
            user: "",//页面得到服务器返回数据之前应认为未登录
            arr: []
        },
        created: function () {
            //发请求得到登录的用户信息
            axios.get("/checkLogin").then(function (response) {
                //如果没登录得到的是一个空字符串 登陆过得到的是一个对象
                vm.user = response.data;

            })
            //发请求获取所有微博数据
            axios.get("/select").then(function (response) {
                vm.arr = response.data;
            })

        },
        methods: {
            logout: function () {
                if (confirm("您确认退出登录吗?")) {
                    axios.get("/logout").then(function () {
                        location.reload();

                    })
                }

            }
        }
    })
</script>
</body>
</html>